LoadPOFile("languages/mamahaha.po", "ch")


PrefabFiles = { "zhubi" }
GLOBAL.setmetatable(env, { __index = function(t, k) return GLOBAL.rawget(GLOBAL, k) end })
STRINGS.NAMES.ZHUBI = "铸币"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.ZHUBI = "吃了就会小累，珍珠币呀！"


local function addPigLoot(inst)
	if inst.components.lootdropper then
		inst.components.lootdropper:AddChanceLoot("zhubi", 1)
	end
end

AddPrefabPostInit("pigman", function(inst)
	if (inst.name == "玛玛哈哈") then
		addPigLoot(inst)
		inst:ListenForEvent("transformwere", addPigLoot)
		inst:ListenForEvent("transformnormal", addPigLoot)
	end
end)
AddPrefabPostInit("pigguard", function(inst)
	if (inst.name == "玛玛哈哈") then
		addPigLoot(inst)
		inst:ListenForEvent("transformwere", addPigLoot)
		inst:ListenForEvent("transformnormal", addPigLoot)
	end
end)
AddPrefabPostInit("moonpig", function(inst)
	if (inst.name == "玛玛哈哈") then
		addPigLoot(inst)
		inst:ListenForEvent("transformwere", addPigLoot)
		inst:ListenForEvent("transformnormal", addPigLoot)
	end
end)

-- 检查别人时说的话
local describeOther = {
	--玛玛哈哈
	["KU_4R9OEUw_"] = {
		"%s？蒸不熟哦，勿cue",
		"%s，你请我吃肯德基疯狂星期四吗？",
		"%s，你请我吃汉堡王周三疯狂国王日吗？",
		"%s，让我们点一波外卖吧",
		"嘻嘻，是铸币%s",
		"让我们来看看铸币%s有没有在直播",
		"%s，现在这个mod多少订阅了？",
	},
	--星河
	["KU_hjXTcv1i"] = {
		"%s，前辈的草甲赐予你力量"
	},
	-- hubert
	["KU_lJILmhD9"] = {
		"%s，来速通吗",
		"速通有无",
	}
}

-- 被检查时说的话
local described = {
	["KU_4R9OEUw_"] = {
		"%s，你答应送我的饥荒全皮肤呢",
		"%s，你怎么夜袭纸灰机的老婆们呢",
	},
	["KU_hjXTcv1i"] = {
		"是草甲前辈%s"
	},
	["KU_lJILmhD9"] = {
		"今天又在迫害谁了",
		"%s 又在写bug了吗"
	},
}

-- 俩人之间的特殊对话，第一级 检查 第二级 时说的话
local relation = {
	["KU_lJILmhD9"] = {
		["KU_4R9OEUw_"] = {
			"是懒狗主播！"
		}
	}
}

-- 这个函数会在每个 player 初始化后执行
AddPlayerPostInit(function(player)
	-- 把之前的描述函数存下来
	local oldGetDescription = player.components and player.components.inspectable and
		player.components.inspectable.getspecialdescription
	-- 如果是空的那就别搞事了
	if not oldGetDescription then
		return
	end
	-- 然后覆盖掉函数
	player.components.inspectable.getspecialdescription = function(inst, viewer)
		local me = viewer.userid
		local other = inst.userid
		local debug_prefix = ""

		-- 测试用
		if (not other or other == "") and me == "KU_lJILmhD9" then
			local others = {
				"KU_lJILmhD9",
				"KU_4R9OEUw_",
				"KU_Y-TuloJ5"
			}
			other = others[math.random(#others)]
			debug_prefix = other .. " "
		end
		if not me or not other then
			return oldGetDescription(inst, viewer)
		end
		local descriptions = nil
		if relation[me] and relation[me][other] then
			descriptions = relation[me][other]
		elseif described[other] then
			descriptions = described[other]
		elseif describeOther[me] then
			descriptions = describeOther[me]
		end
		if descriptions and #descriptions > 0 then
			local description = descriptions[math.random(#descriptions)]
			if string.find(description, "%s", 1, true) then
				return debug_prefix .. string.format(
					description,
					inst:GetDisplayName()
				)
			else
				return debug_prefix .. description
			end
		else
			return oldGetDescription(inst, viewer)
		end
	end
end)

function MapUserID(descriptions, userIdAlias)
	local t = {}
	for key, value in pairs(descriptions) do
		key = userIdAlias[key] or key
		t[key] = value
	end
	return t
end

function UpdateInspectionData()
	print("Updating Inspection Data")
	TheSim:QueryServer("https://gitee.com/hubertzhang/dst-mayanmayu/raw/inspection-data/inspections.lua",
		function(result, isSuccessful, resultCode)
			print("Request get result,successful:", isSuccessful, "code:", resultCode)
			if isSuccessful and string.len(result) > 1 and resultCode == 200 then
				local error = {}
				local status, data = pcall(loadstring(result))
				if not status or not data then
					print("Failed to load inspections.lua")
					return
				end
				local userIdAlias = {}
				if data["userIdAlias"] then
					userIdAlias = data["userIdAlias"]
				end
				if data["describeOther"] then
					describeOther = MapUserID(data["describeOther"], userIdAlias)
				end
				if data["described"] then
					described = MapUserID(data["described"], userIdAlias)
				end
				if data["relation"] then
					local t = {}
					for key, value in pairs(data["relation"]) do
						key = userIdAlias[key] or key
						t[key] = MapUserID(value, userIdAlias)
					end
					relation = t
				end
				print("Remote Inspection Loaded!")
			end
		end,
		"GET"
	)
end

UpdateInspectionData()

GLOBAL.UpdateInspectionData = UpdateInspectionData
